package com.cema.manage.project.manage.proCategory.controller;
import java.util.ArrayList;
import java.util.List;
import com.cema.manage.common.utils.StringUtils;
import com.cema.manage.project.manage.proCategory.domain.ProCategory;
import com.cema.manage.project.manage.proCategory.service.IProCategoryService;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.cema.manage.framework.web.controller.BaseController;
import com.cema.manage.framework.web.domain.Message;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.cema.manage.framework.web.page.TableDataInfo;


/**
 * 分类管理 信息操作处理
 *
 * @author reasahi
 * @date 2024-10-12
 */
@Controller
@RequestMapping("/manage/proCategory")
public class ProCategoryController extends BaseController {
    private String prefix = "manage/proCategory";

    @Autowired
    private IProCategoryService proCategoryService;

    @GetMapping()
    @RequiresPermissions("manage:proCategory:view")
    public String proCategory() {
        return prefix + "/proCategory";
    }

    /**
     * 查询分类管理列表
     */
    @RequiresPermissions("manage:proCategory:list")
    @ResponseBody
    @GetMapping("/list")
    public TableDataInfo list(ProCategory proCategory) {
        startPage();
        EntityWrapper entityWrapper = new EntityWrapper();
        entityWrapper.setEntity(proCategory);
        entityWrapper.orderBy("pc_sort", true);
        List<ProCategory> list = proCategoryService.selectList(entityWrapper);
        return getDataTable(list);
    }

    /**
     * 新增分类管理
     */
    @RequiresPermissions("manage:proCategory:add")
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 修改分类管理
     */
    @RequiresPermissions("manage:proCategory:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, Model model) {
        ProCategory proCategory =proCategoryService.selectById(id);
        model.addAttribute("proCategory", proCategory);
        return prefix + "/edit";
    }

    /**
     * 保存分类管理
     */
    @RequiresPermissions("manage:proCategory:save")
    @PostMapping("/save")
    @ResponseBody
    public Message save(ProCategory proCategory) {
        Integer id = proCategory.getId();
        if (StringUtils.isNotNull(id)) {
            if (proCategoryService.updateById(proCategory)) {
                return Message.success();
            }
            return Message.error();
        } else {
            if (proCategoryService.insert(proCategory)) {
                return Message.success();
            }
            return Message.error();
        }
    }

    /**
     * 删除分类管理
     */
    @RequiresPermissions("manage:proCategory:remove")
    @PostMapping("/remove/{id}")
    @ResponseBody
    public Message remove(@PathVariable("id") Integer id) {
        if (proCategoryService.deleteById(id)) {
            return Message.success();
        }
        return Message.error();
    }

    /**
     * 批量删除分类管理
     */
    @RequiresPermissions("manage:proCategory:batchRemove")
    @PostMapping("/batchRemove")
    @ResponseBody
    public Message remove(@RequestParam("ids[]") Integer[]ids) {
        List list = new ArrayList();
        for (Integer n:ids) {
            list.add(n);
        }
        if (proCategoryService.deleteBatchIds(list)) {
            return Message.success();
        }
        return Message.error();
    }

}
